#include <cstdio> 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m;
int a[1000010];
int b[10010];
int nxt[10010];
void getnxt()
{
	int t,i;
	t = 0;
	i = 1;
	nxt[1] = 0;
	while(i<=m)
	{
		if(t==0||b[t]==b[i])
		{
			nxt[i+1] = t+1;
			i++;
			t++;
		}
		else
		{
			t = nxt[t];
		}
	}
}
int kmp()
{
	getnxt();
	int i = 1;
	int j = 1;
	while(i<=n)
	{
		if(j==0||a[i]==b[j])
		{
			i++;
			j++;
		}
		else
		{
			j = nxt[j];
		}
		if(j == m+1)
		{
			return i-m;
		}
	}
	return -1;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		for(int i = 1;i<=n;i++) scanf("%d",&a[i]);
		for(int i =1;i<=m;i++)scanf("%d",&b[i]);
		int ans = kmp();
		printf("%d\n",ans);
	}
}
